#include <string.h>

#include "AncestorMol.h"
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
AncestorMol::AncestorMol() {
  this->smile           = new SmileStruct();
  this->listIdAncestor  = new TListE <String> ();
}
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
AncestorMol::AncestorMol(SmileStruct *smile) {
  this->smile           = new SmileStruct(*smile);
  this->listIdAncestor  = new TListE <String> ();
}
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
AncestorMol::AncestorMol(AncestorMol* ancestorMol) {
  this->smile           = new SmileStruct(*(ancestorMol->smile));
  this->listIdAncestor  = new TListE <String> (ancestorMol->listIdAncestor);
}
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
AncestorMol::AncestorMol(const AncestorMol& ancestorMol) {
  this->smile           = new SmileStruct(*(ancestorMol.smile));
  this->listIdAncestor  = new TListE <String> (ancestorMol.listIdAncestor);
}
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
AncestorMol::~AncestorMol() {
  if (this->smile)          delete this->smile;
  if (this->listIdAncestor) delete this->listIdAncestor;
}
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
void AncestorMol::Smile(SmileStruct *smile) {
  if (!smile)      return;
  *(this->smile) = *smile;
}
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
void AncestorMol::ListIdAncestor(TListE <String>* listIdAncestor) {
  if (!listIdAncestor) return;
  *(this->listIdAncestor) = *(listIdAncestor);
}
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
SmileStruct* AncestorMol::Smile() {
  return this->smile;
}
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
TListE <String>* AncestorMol::ListIdAncestor() {
  return this->listIdAncestor;
}
//---------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------
void AncestorMol::InputLineSmile(String line) {
  this->smile->InputLine(line);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void AncestorMol::InputLineSmile(char* line) {
  this->smile->InputLine(line);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void AncestorMol::InputLineAncestor(String line) {

  this->listIdAncestor->Clear();

  char *part = strtok(line.Out(), " \t\n\r");

  while (part) {
    String *temporal = new String(part);
    this->listIdAncestor->Add(temporal);
    part = strtok(NULL, " \t\n\r");
  }
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void AncestorMol::InputLineAncestor(char* line) {

  this->listIdAncestor->Clear();

  char *part = strtok(line, " \t\n\r");

  while (part) {
    String *temporal = new String(part);
    this->listIdAncestor->Add(temporal);
    part = strtok(NULL, " \t\n\r");
  }

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
char* AncestorMol::OutToAncestorString() {

  int higherLength = 0;

  this->listIdAncestor->SetInitial();
  for (int i = 0; i < this->listIdAncestor->Length(); i++)
    higherLength = (this->listIdAncestor->GetNext()->Length() > higherLength)?this->listIdAncestor->GetCurrent()->Length():higherLength;

  char *StringOut = new char[(higherLength + 4) * this->listIdAncestor->Length() + 1]; StringOut[0] = '\0';

  this->listIdAncestor->SetInitial();
  for (int i = 0; i < this->listIdAncestor->Length(); i++)
    sprintf(StringOut, "%s%*s    \0", StringOut, higherLength, this->listIdAncestor->GetNext()->Out());

  StringOut[(higherLength + 4) * this->listIdAncestor->Length()] = '\0';

  return StringOut;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
char* AncestorMol::OutToSmileString() {
  return this->smile->OutToSmileString();
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
char* AncestorMol::OutToSmileStringWithInfo() {
  return this->smile->OutToSmileStringWithInfo();
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
AncestorMol AncestorMol::operator=(const AncestorMol& ancestorMol) {

  *(this->smile)          = *(ancestorMol.smile);
  *(this->listIdAncestor) = *(ancestorMol.listIdAncestor);

  return *this;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
